package com.alibaba.citrus.ots.platform.enhance.facade.service;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.platform.enhance.repository.DictionaryRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.dictionarysave.service.DictionarySaveService;
import com.epoch.app.otsplatformenhance.model.dto.*;
import com.epoch.app.otsplatformenhance.sdo.DictionarySDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/10/17 15:50
 */
@Primary
@Service
public class DictionarySaveServiceImpl implements DictionarySaveService {
    @Autowired
    private DictionaryRepository dictionaryRepository;
    @Override
    @FacadeInvoker
    public Result<String> createDict(CreateDictionaryRequest request) {
        QueryDictionaryRequest queryDictionaryRequest = new QueryDictionaryRequest();
        queryDictionaryRequest.setDictCode(request.getCode());
        DictionarySDO sdo = dictionaryRepository.querySingle(queryDictionaryRequest);
        if (sdo != null){
            throw new RuntimeException("字典编码不能重复");
        }
        String id = dictionaryRepository.createDictionary(request);
        return Result.success(id);
    }

    @Override
    @FacadeInvoker
    public Result batchCreateDict(BatchCreateDictRequest batchCreateDictRequest) {
        List<CreateDictionaryRequest> batchRequest = batchCreateDictRequest.getBatchRequest();
        if (CollectionUtils.isNotEmpty(batchRequest)) {
            batchRequest.forEach(this::createDict);
        }
        return Result.success(null);
    }

    @Override
    @FacadeInvoker
    public Result createDictValue(CreateDictValueRequest request) {
        String id = dictionaryRepository.createDictValue(request);
        return Result.success(id);
    }

    @Override
    @FacadeInvoker
    public Result updateDict(UpdateDictCodeRequest request) {
        return null;
    }

    @Override
    @FacadeInvoker
    public Result updateDictValue(UpdateDictValueRequest request) {
        return null;
    }
}
